package org.jeecg.modules.bobo.wechat.common.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.bobo.wechat.common.properties.WxConfigProperties;
import org.jeecg.modules.bobo.wechat.miniapp.properties.WxminiappProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 微信公众号主配置
 * <p>
 * Created by bjliumingbo on 2017/5/12.
 *
 * @author FirenzesEagle
 * @author BinaryWang
 */
@Configuration
@AutoConfigureOrder(1)
@EnableConfigurationProperties({WxConfigProperties.class, WxminiappProperties.class})
public class WxConfiguration {
    @Autowired
    private WxConfigProperties wxConfigProperties;
    @Autowired
    private WxminiappProperties wxminiappProperties;

    @Bean
    @ConditionalOnMissingBean(WxMpConfigStorage.class)
    public WxMpConfigStorage wxMpConfigStorage() {
        WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
        configStorage.setAppId(this.wxConfigProperties.getAppId());
        configStorage.setSecret(this.wxConfigProperties.getAppSecret());
        configStorage.setToken(this.wxConfigProperties.getToken());
        configStorage.setAesKey(this.wxConfigProperties.getAesKey());
        return configStorage;
    }

    @Bean
    @ConditionalOnMissingBean(WxMpService.class)
    public WxMpService wxMpService() {
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }
    @Bean
    @ConditionalOnMissingBean(WxMaService.class)
    public WxMaService wxMaService() {
            WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
            config.setAppid(wxminiappProperties.getAppid());
            config.setSecret(wxminiappProperties.getSecret());
            config.setToken(wxminiappProperties.getToken());
            config.setAesKey(wxminiappProperties.getAesKey());
            config.setMsgDataFormat(wxminiappProperties.getMsgDataFormat());
            WxMaService service = new WxMaServiceImpl();
            service.setWxMaConfig(config);
            return service;
    }
//    @Bean
//    @ConditionalOnMissingBean(WxMaQrcodeService.class)
//    public WxMaQrcodeService wxMaQrcodeService() {
//            WxMaQrcodeService wxMaQrcodeService = new WxMaQrcodeServiceImpl(wxMaService());
//            return wxMaQrcodeService;
//    }


    @Bean
    @ConditionalOnMissingBean(WxPayService.class)
    public WxPayService wxPayService() {
        WxPayConfig payConfig = new WxPayConfig();
//        payConfig.setAppId(StringUtils.trimToNull(this.wxConfigProperties.getAppId()));
        //这里appid使用小程序的appid
        payConfig.setAppId(StringUtils.trimToNull(this.wxminiappProperties.getAppid()));

        payConfig.setMchId(StringUtils.trimToNull(this.wxConfigProperties.getMchId()));
        payConfig.setMchKey(StringUtils.trimToNull(this.wxConfigProperties.getMchKey()));
//        payConfig.setSubAppId(StringUtils.trimToNull(this.wxConfigProperties.getSubAppId()));
//        payConfig.setSubMchId(StringUtils.trimToNull(this.wxConfigProperties.getSubMchId()));
        payConfig.setKeyPath(StringUtils.trimToNull(this.wxConfigProperties.getKeyPath()));

        // 可以指定是否使用沙箱环境
        payConfig.setUseSandboxEnv(false);
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        if (payConfig.isUseSandboxEnv()){
            try {
                wxPayService.getConfig().setMchKey(StringUtils.trimToNull(wxPayService.getSandboxSignKey()));
            } catch (WxPayException e) {
                e.printStackTrace();
            }
        }
        return wxPayService;
    }

}
